﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;

namespace ProjetoFase2
{
    public partial class SyncForm : Form
    {
        private int port;
        private string ip;
        private TcpClient clientSocket;
        private Thread syncThread;
        private NetworkStream stream;
        private DataSet receivedDataSet;

        public SyncForm(string ip, int port)
        {
            InitializeComponent();
        }

        private void menuClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSync_Click(object sender, EventArgs e)
        {
            syncThread = new Thread(new ThreadStart(Sync));
            syncThread.Start();
        }

        private void Sync()
        {
            btnSync.Enabled = false;

            ServerInteraction servInt = new ServerInteraction(null, 0);
            clientSocket = new TcpClient(ip, port);

            if (clientSocket.Client.Connected)
            {
                // perform sync operations
                stream = new NetworkStream(clientSocket);
                traineeDataSet = servInt.writeDataSet(stream, Residence.listaInativa);
                receivedDataSet = servInt.readDataSet(stream);
                // save data to database and load datasets to show the user what has been accepted
                // and wat was not accepted
            }

            btnSync.Enabled = true;
        }
    }
}